# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=uncrustify
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.71.0
pkgrel=1
pkgdesc="Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA (mingw-w64)"
arch=(any)
url="http://uncrustify.sourceforge.net/"
license=(GPL)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/uncrustify/uncrustify/archive/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-install-docs.patch")
sha512sums=('220ecbebb1c1d6abadb96c339e4ca9b264a04fa08bc83e9b0fabba5fae425efd1911d175d18b635521614ec9c33aa389fadaeec62ebf28a5d71e595900a92181'
            'b419ecd625145ef2ad841ead56992cf5c0ff758e81a6e29089789aa3ed38ea20cd636951e6563a215a8674601060c624f494d233e24a3fa379ba9836db6ee3b8')

prepare() {
  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  patch -Nbp1 -i "${srcdir}"/0001-cmake-install-docs.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  cd ${srcdir}/${_realname}-${_realname}-${pkgver}
  install -d -m755 "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
  install -m644 etc/*.cfg "$pkgdir"/${MINGW_PREFIX}/share/uncrustify
}
